package com.jjc.android.view

import android.content.Context
import android.graphics.Color
import android.view.View
import android.widget.TextView
import com.github.mpcharting.components.MarkerView
import com.github.mpcharting.data.Entry
import com.github.mpcharting.highlight.Highlight
import com.github.mpcharting.utils.MPPointF
import com.jjc.android.R
import com.jjc.android.entity.DetectionReportInfo
import com.jjc.android.entity.HealthRecordDetailsItem
import com.jjc.android.util.Common.parseEmpty
import com.jjc.android.util.dp

class DetailMarkerView(context: Context) : MarkerView(context, R.layout.view_chart_detail_marker){

    init {
        setOffset(10.dp.toFloat(), -14.dp.toFloat())
    }

    private var isShowSingleValue = false
    fun setShowSingleValue(isShowSingleValue:Boolean){
        this.isShowSingleValue = isShowSingleValue
    }

    var reportInfo: DetectionReportInfo? = null

    override fun refreshContent(e: Entry?, highlight: Highlight?) {
        (e?.data as? HealthRecordDetailsItem).let { info->
            if(info!=null){
                findViewById<View>(R.id.contentLayout).visibility = View.VISIBLE
                findViewById<TextView>(R.id.timeTxt).text = info.collectDate.parseEmpty()
                findViewById<TextView>(R.id.resultTxt1).text = reportInfo?.minorTermEnglishName.parseEmpty()
                findViewById<TextView>(R.id.resultUnitTxt1).text = info.adjustedResDisplay.parseEmpty()
                findViewById<TextView>(R.id.resultTxt2).text = reportInfo?.minorTermEnglishName.parseEmpty()
                findViewById<TextView>(R.id.resultUnitTxt2).text = info.primaryResDisplay.parseEmpty()
                findViewById<View>(R.id.line2Info).visibility = if(isShowSingleValue) View.GONE else View.VISIBLE
            }else{
                findViewById<View>(R.id.contentLayout).visibility = View.INVISIBLE
            }

        }
        super.refreshContent(e, highlight)
    }

    override fun getOffsetForDrawingAtPoint(posX: Float, posY: Float): MPPointF {
        val offset = offset
        mOffset2.x = offset.x
        mOffset2.y = offset.y

        val chart = chartView

        val width = width.toFloat()
        val height = height.toFloat()

        if (posX + mOffset2.x < 0) {
            mOffset2.x = -posX
        } else if (chart != null && posX + width + mOffset2.x > chart.width) {
            mOffset2.x = -(width+offset.x)
        }

        if (posY + mOffset2.y < 0) {
            mOffset2.y = -posY
        } else if (chart != null && posY + height + mOffset2.y > chart.height) {
            mOffset2.y = chart.height - posY - height
        }

        return mOffset2
    }

}